package com.zlzxm.shoesmarket.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.adapter.ADCalculateSize;
import com.zlzxm.shoesmarket.view.adapter.ADCalculateWholesaleInfo;
import com.zlzxm.shoesmarket.view.window.WindowCalculateShare;
import com.zlzxm.shoesmarket.viewmodel.VMWholesaleCalculate;

/**
 * CREATE BY zlzxm ON 4/26/21
 * <br>
 *批发计算器
 */
public class FMWholesaleCalculate extends BaseFragment implements OnItemClickListener,View.OnClickListener {



    VMWholesaleCalculate vmWholesaleCalculate;

    ADCalculateSize adCalculateSize = new ADCalculateSize();

    ADCalculateWholesaleInfo adCalculateWholesaleInfo = new ADCalculateWholesaleInfo();

    private RecyclerView rvSize,rvInfo;




    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fm_wholesale_calculate,container,false);
    }

    @Override
    protected void initView(@NonNull View view, @NonNull Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
        rvSize = ZViewHelp.findById(view,R.id.rvSize);


        rvSize.setLayoutManager(new GridLayoutManager(getContext(),5));


        rvInfo = ZViewHelp.findById(view,R.id.rvInfo);


        rvInfo.setLayoutManager(new LinearLayoutManager(getContext()));

        adCalculateSize.setOnItemClickListener(this);

        ZViewHelp.setOnClickListener(view,R.id.ivShare,this);
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        vmWholesaleCalculate = new ViewModelProvider(this).get(VMWholesaleCalculate.class);

        vmWholesaleCalculate.getLiveSize().observe(this,adCalculateSize::setList);
        vmWholesaleCalculate.getLiveInfo().observe(this,adCalculateWholesaleInfo::setList);


    }

    @Override
    protected void initData() {
        super.initData();
        rvSize.setAdapter(adCalculateSize);
        rvInfo.setAdapter(adCalculateWholesaleInfo);
        vmWholesaleCalculate.requestSize();
        vmWholesaleCalculate.requestInfo();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

        adCalculateSize.setSelectPosition(position);
    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.ivShare){

            showWindow();
        }

    }


    private void showWindow(){

        WindowCalculateShare.newInstance()
                .show(getChildFragmentManager(),"");


    }



}


